/*
 * Copyright 2008 Georgi Staykov
 * 
 * This file is part of pscoder.
 *
 * pscoder is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * pscoder is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with pscoder.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.gstaykov.pscoder.preferences;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.ColorFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.gstaykov.pscoder.Activator;
import com.gstaykov.pscoder.editor.syntax.PowershellColors;

public class PowershellPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

    public PowershellPreferencePage() {
        super(GRID);
        setPreferenceStore(Activator.getDefault().getPreferenceStore());
        setDescription("Powershell preference page");
    }
    
	protected void createFieldEditors() {
	    addField(new ColorFieldEditor(PreferenceConstants.COLOR_COMMENT, "Comment coloring (# comment)", getFieldEditorParent()));
        addField(new ColorFieldEditor(PreferenceConstants.COLOR_DASH_KEYWORD, "-Keyword coloring (-and, -or, etc.)", getFieldEditorParent()));
        addField(new ColorFieldEditor(PreferenceConstants.COLOR_KEYWORD, "Keyword coloring (if, for, etc.)", getFieldEditorParent()));
        addField(new ColorFieldEditor(PreferenceConstants.COLOR_STRING, "String coloring (\"String\" or 'String')", getFieldEditorParent()));
        addField(new ColorFieldEditor(PreferenceConstants.COLOR_VARIABLE, "Variable coloring ($var)", getFieldEditorParent()));
        addField(new ColorFieldEditor(PreferenceConstants.COLOR_OTHER, "General text coloring (everything else)", getFieldEditorParent()));
	    
	    // Tab width
	    IntegerFieldEditor tabWidth = new IntegerFieldEditor(PreferenceConstants.TAB_WIDTH, "Tab width", getFieldEditorParent());
	    tabWidth.setValidRange(0, 8);
	    addField(tabWidth);
	    
	    // Include powershell cmdlets
	    addField(new BooleanFieldEditor(PreferenceConstants.INCLUDE_PSCMDLETS, "Include Powershell cmdlets", getFieldEditorParent()));
	    
	    // Include custom snapins
	    addField(new BooleanFieldEditor(PreferenceConstants.INCLUDE_CUSTOMCMDLETS, "Include custom added snapins", getFieldEditorParent()));
	    
	    // PS Snapin file list
	    addField(new FileEditor(PreferenceConstants.PS_SNAPIN_FILES, "Custom snapins", getFieldEditorParent(), new String[] {"*.dll"}));
	}

	public void init(IWorkbench workbench) {

	}

	@Override
	protected IPreferenceStore doGetPreferenceStore() {
	    return Activator.getDefault().getPreferenceStore();
	}
	
	@Override
	public boolean performOk() {
	    boolean success = super.performOk();
	    PowershellColors.reloadPreferences(); // FIXME: maybe utility method is better
	    return success;
	}
}
